<?php

function sort_links($a, $b) {
    $sort_order =& $a['sort_order'];
    return strcmp($a["link_$sort_order"], $b["link_$sort_order"]);
}


require_once 'classes/LightPressPlugin.php';

class Links extends LightPressPlugin {
    
    var $constructor_args = array(
        'title'=>'sidebar block title',
        'use_feeds'=>'display feeds\' XML icon');
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'Show the links defined in WP';
    
    var $active = true;
    var $hooks = array('sidebar');
    
    var $title = 'Links';
    var $use_feeds = false;
    
    function Links(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        $this->use_feeds = (int)$this->use_feeds;
    }
    
    function run($hook, &$hook_data) {
        $db =& $this->_frontend->db;
        $tpl =& $this->_frontend->tpl;
        $tables =& $this->_frontend->tables;
        
        if ($this->use_feeds)
            $tpl->setFile('plugin_links', 'plugins/links_feeds.xml');
        else
            $tpl->setFile('plugin_links', 'plugins/links.xml');
        $tpl->setBlock('plugin_links', 'plugin_links_l', 'PLUGIN_LINKS_L');
        if ($this->use_feeds)
            $tpl->setBlock('plugin_links_l', 'plugin_links_l_feed', 'PLUGIN_LINKS_L_FEED');
        
        $q = "select
                l.link_name, l.link_url, l.link_description,
                l.link_image, l.link_rss, l.link_rating, l.link_updated, 
                c.cat_id, c.cat_name, c.show_description, c.sort_order,
                c.sort_desc, c.list_limit
            from
                " . $tables['links'] . " l
            inner join
                " . $tables['linkcategories'] . " c on l.link_category=c.cat_id
            where
                l.link_visible='Y'
            order
                by c.cat_id, l.link_id";
             
        $num_records = $db->query($q);
        
        if ($num_records == 0) {
            $tpl->setVar('PLUGIN_LINKS', "");
            return;
        }
                
            
        $links = array();
        $cat_name = null;
        $cat_id = null;
        $rows = array();
        
        foreach ($db->all() as $link) {
            if ($link['cat_name'] != $cat_name) {
                $this->parse_link_category($tpl, $cat_name, $rows, $links, $cat_id, $this->use_feeds);
                $rows = array();
                $cat_name = $link['cat_name'];
                $cat_id = $link['cat_id'];
            }
            $rows[] = $link;
        }
        $this->parse_link_category($tpl, $cat_name, $rows, $links, $cat_id, $this->use_feeds);
    }
    
    function parse_link_category(&$tpl, &$cat_name, &$rows, &$links, $cat_id, $use_feeds) {
        if (count($rows) > 0) {
            $row =& $rows[0];
            $sort_order =& $row['sort_order'];
            if ($sort_order == 'rand') {
                shuffle($rows);
            } else if (!empty($sort_order) && isset($row["link_$sort_order"])) {
                usort($rows, 'sort_links');
                if ($row['sort_desc'] == 'Y')
                    $rows = array_reverse($rows);
            }
            $list_limit = (int)$row['list_limit'];
            if ($list_limit > 0)
                $rows = array_slice($rows, 0, $list_limit);
            $tpl->setVar(array('plugin_links_cat_name'=>$cat_name, 'plugin_links_cat_id'=>$cat_id));
                
            if ($use_feeds) {
                $show_description =& $row['show_description'];
                $append = false;
                foreach ($rows as $link) {
                    $tpl->setVar(array(
                        'plugin_links_l_link_name'          => $link['link_name'],
                        'plugin_links_l_link_url'           => $link['link_url'],
                        'plugin_links_l_link_description'   => ($show_description ? $link['link_description'] : ''),
                        'plugin_links_l_link_image'         => $link['link_image'],
                        'plugin_links_l_link_rss'           => $link['link_rss'],
                        'plugin_links_l_link_rating'        => $link['link_rating'],
                        'plugin_links_l_link_updated'       => $link['link_updated']));
                        
                    if (empty($link['link_rss'])) {
                        $tpl->setVar('PLUGIN_LINKS_L_FEED', '');
                    } else {
                        $tpl->parse('PLUGIN_LINKS_L_FEED', 'plugin_links_l_feed');
                    }
                    $tpl->parse('PLUGIN_LINKS_L', 'plugin_links_l', $append);
                    $append = true;
                }
            } else {
                $tpl->parseBlock('PLUGIN_LINKS_L', 'plugin_links_l', $rows);
            }
                
            $tpl->parse('PLUGIN_LINKS', 'plugin_links', true);
        }
    }
        
}
        

?>
